package controller.Supper;

import com.sun.org.apache.xml.internal.dtm.ref.sax2dtm.SAX2DTM2;
import pojo.All;
import service.Supper.SuperGoodsService;
import service.Supper.impl.SuperGoodsServiceImpl;
import util.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "SuperGoodsServlet",urlPatterns = "/goodsServlet")
public class SuperGoodsServlet extends HttpServlet {
    Long id ;   //定义全局变量id
    Long bigId;   //定义全部变量大分类id
    String smallName;  //定义全局变量小分类名称
    String goodsName;   //商品名称
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        SuperGoodsService sgs = new SuperGoodsServiceImpl();
        String action = request.getParameter("action");
        int pageSize =5;
        if(action!=null &&"goodsByBig".equals(action)){   //进入头部信息查询
            if (request.getParameter("id") != null) {
               this.id = Long.valueOf(request.getParameter("id"));
            }
            int currentPage = Integer.valueOf(request.getParameter("pageNumber"));   //当前页数
            int totalCount = sgs.getTotalCount(id,null,null);   //得到总数据行
            //存放Page
            PageSupport pageSupport = new PageSupport();
            pageSupport.setPageSize(pageSize);
            pageSupport.setCurrentPage(currentPage);
            pageSupport.setTotalCount(totalCount);
            pageSupport.setTotalPages(pageSupport.getTotalPages());
            List<All> alls = sgs.selGoods(currentPage,pageSize,id,null,null);
            pageSupport.setList(alls);
            request.getSession().setAttribute("pageSupport",pageSupport);
            request.getSession().setAttribute("servletURL","goodsServlet?action=goodsByBig");
           // response.sendRedirect("/BeforePage/GUIMEI/dianzichangping.jsp");
            request.getRequestDispatcher("/BeforePage/GUIMEI/dianzichangping.jsp").forward(request,response);
       }else if("goodsLookByIg".equals(action)){   //根据id进行查看商品信息
            Long goodId = Long.valueOf(request.getParameter("id"));
            All all = sgs.SelById(goodId);
            request.getSession().setAttribute("All",all);
           // response.sendRedirect("/BeforePage/GUIMEI/info.jsp");
            request.getRequestDispatcher("/BeforePage/GUIMEI/info.jsp").forward(request,response);
        }else if("goodsCusQuery".equals(action)){    //进入中间部分显示
            int currentPage = Integer.valueOf(request.getParameter("pageNumber"));   //当前页数
            int totalCount = sgs.getTotalCount(Long.valueOf("-1"),null,null);   //得到总数据行
            //存放Page
            PageSupport pageSupport = new PageSupport();
            pageSupport.setPageSize(12);
            pageSupport.setCurrentPage(currentPage);
            pageSupport.setTotalCount(totalCount);
            pageSupport.setTotalPages(pageSupport.getTotalPages());
            List<All> alls = sgs.selGoods(currentPage,12,Long.valueOf("-1"),null,null);
            pageSupport.setList(alls);
            request.getSession().setAttribute("pageSupport",pageSupport);
            request.getSession().setAttribute("servletURL","goodsServlet?action=goodsCusQuery");
            // response.sendRedirect("/BeforePage/GUIMEI/dianzichangping.jsp");
            request.getRequestDispatcher("/BeforePage/GUIMEI/goodsQuery.jsp").forward(request,response);
        }else if("goodsQueryNew".equals(action)){    //进入新品上架查询
            //存放Page
            PageSupport pageSupport = new PageSupport();
            List<All> alls = sgs.selGoods(1,4,Long.valueOf("-1"),null,null);
            pageSupport.setList(alls);
            request.getSession().setAttribute("pageSu",pageSupport);
            // response.sendRedirect("/BeforePage/GUIMEI/dianzichangping.jsp");
            request.getRequestDispatcher("/BeforePage/GUIMEI/goodsQueryNew.jsp").forward(request,response);
        }else if("goodsSmallQuery".equals(action)){    //左边小商品点击查询
            if (request.getParameter("id") != null) {
                this.bigId = Long.valueOf(request.getParameter("id"));
            }
            if(request.getParameter("smallName")!=null){
                this.smallName = request.getParameter("smallName");
            }

            int currentPage = Integer.valueOf(request.getParameter("pageNumber"));   //当前页数
            int totalCount = sgs.getTotalCount(bigId,smallName,null);   //得到总数据行
            //存放Page
            PageSupport pageSupport = new PageSupport();
            pageSupport.setPageSize(5);
            pageSupport.setCurrentPage(currentPage);
            pageSupport.setTotalCount(totalCount);
            pageSupport.setTotalPages(pageSupport.getTotalPages());
            List<All> alls = sgs.selGoods(currentPage,5,bigId,smallName,null);
            pageSupport.setList(alls);
            request.getSession().setAttribute("pageSupport",pageSupport);
            request.getSession().setAttribute("servletURL","goodsServlet?action=goodsSmallQuery");
            // response.sendRedirect("/BeforePage/GUIMEI/dianzichangping.jsp");
            request.getRequestDispatcher("/BeforePage/GUIMEI/dianzichangping.jsp").forward(request,response);
        }else if("goodsCusQueryLike".equals(action)){   //搜索
            if(request.getParameter("goodsName")!=null){
                this.goodsName = request.getParameter("goodsName");
            }
            int currentPage = Integer.valueOf(request.getParameter("pageNumber"));   //当前页数
            int totalCount = sgs.getTotalCount(Long.valueOf("-1"),null,goodsName);   //得到总数据行
            //存放Page
            PageSupport pageSupport = new PageSupport();
            pageSupport.setPageSize(5);
            pageSupport.setCurrentPage(currentPage);
            pageSupport.setTotalCount(totalCount);
            pageSupport.setTotalPages(pageSupport.getTotalPages());
            List<All> alls = sgs.selGoods(currentPage,5,Long.valueOf("-1"),null,goodsName);
            pageSupport.setList(alls);
            request.getSession().setAttribute("pageSupport",pageSupport);
            request.getSession().setAttribute("servletURL","goodsServlet?action=goodsCusQueryLike");
            // response.sendRedirect("/BeforePage/GUIMEI/dianzichangping.jsp");
            request.getRequestDispatcher("/BeforePage/GUIMEI/dianzichangping.jsp").forward(request,response);
        }
    }
}
